package com.lxit.video.service.impl;


import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lxit.video.bean.VideoComment;
import com.lxit.video.dao.VideoCommentRepository;
import com.lxit.video.service.VideoCommentService;
import com.lxit.video.util.PageUtil;

@Service
public class VideoCommentServiceImpl implements VideoCommentService {
	@Autowired
	private VideoCommentRepository videoCommentRepository;

	@Transactional
	public boolean saveVideoComment(Integer userId, String commentDetails, Integer videoId) {
		try {
			videoCommentRepository.saveVideoComment(userId, commentDetails, videoId);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public PageUtil<VideoComment> pageBackstageVideoComment(Integer pageIndex, Integer pageSize, String userNumber,
			String videoName,String stratTime, String entTime) {
		int total = videoCommentRepository.pageBackstageVideoCommentCount(userNumber, videoName, stratTime, entTime);
		if (total > 0) {
			PageUtil<VideoComment> pageUtil = new PageUtil<>(pageIndex, pageSize, total);
			pageUtil.setRows(videoCommentRepository.pageBackstageVideoComment(pageUtil.getPageIndex(), pageSize,
					userNumber, videoName, stratTime, entTime));
			return pageUtil;
		}
		return null;
	}

	@Override
	public PageUtil<VideoComment> pageVideoComment(Integer pageIndex, Integer pageSize, Integer videoId) {
		int total = videoCommentRepository.pageVideoCommentCount(videoId);
		if (total > 0) {
			PageUtil<VideoComment> pageUtil = new PageUtil<>(pageIndex, pageSize, total);
			pageUtil.setRows(videoCommentRepository.pageVideoComment(pageUtil.getPageIndex(), pageSize, videoId));
			return pageUtil;
		}
		return null;
	}

	
	@Override
	@Transactional
	public boolean updateVideoComment(Integer commentId) {
		VideoComment videoComment = videoCommentRepository.findOne(commentId);
		if (videoComment != null) {
			videoComment.setCommentStatus((videoComment.getCommentStatus() == 1) ? 0 : 1);
			return true;
		}
		return false;
	}
}
